/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"4CDDEBFD-8A25-4408-8DCA-04C1C3232F53",variableType:4}
 */
var fListaNoImprimir = 0;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"9AD7BB1C-3861-4186-B072-EE8A176F7187",variableType:4}
 */
var fListaSubCuentas = 0;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"46B99872-25AA-4962-BE49-8AA0A3F2A64E"}
 */
var ftexto_buscar = null;

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"4BFF24BC-2CB4-4A55-A419-BEBD3F471019"}
 */
function onAction_ira_detalle(event) {
	forms.cuentas_det.controller.loadRecords(cuenta_id)
	globals.formEnVentana('cuentas_det','CUENTAS','Detalle de Cuenta')
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"20B3556B-B1DC-47ED-93C9-0E7F46B62EE5"}
 * @AllowToRunInFind
 */
function onAction_buscar(event) {
	var estado = controller.find()
	if (estado == false)
	{
		globals.DIALOGS.showErrorDialog("Error","Error en find")
		return
	}
		subcuenta = 0
		descripcion = "#%" + ftexto_buscar + "%"
		controller.newRecord()
		subcuenta = 0
		cuenta_formato = "%" + ftexto_buscar + "%"
		controller.newRecord()
		subcuenta = 0
		cuenta_busca = "%" + ftexto_buscar + "%"
	controller.search()
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"20A45EF9-7D89-45DA-B4C7-8B4EB6FFC72A"}
 * @AllowToRunInFind
 */
function onShow(firstShow, event) {
	controller.loadRecords(null)
	if (controller.getMaxRecordIndex() != 0) {
		controller.sort("cuenta_formato asc")	
	}
	globals.gvarios = 5
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"5383A1F1-7348-40D7-BD47-4685954069F2"}
 * @AllowToRunInFind
 */
function onAction_todos(event) {
	databaseManager.revertEditedRecords()
	var estado = controller.find()
	if (estado == false)
	{
		globals.DIALOGS.showWarningDialog("Aviso","No se pudo cargar todos los registros")
		return
	}
	subcuenta = 0
	controller.search()
	ftexto_buscar = null
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"3E23D668-B9F5-45C6-A1E3-2FC484856A1C"}
 */
function imprimir(event) {
/*	var $cuentas_lista_plan = forms.cuentas_lista_plan
	Antes_imp($cuentas_lista_plan)
	$cuentas_lista_plan.controller.showPrintPreview(false, null, 100)*/
	
	jasper('impresion')
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"24843891-AA62-4C9F-9779-CE9146525EF4"}
 */
function pdf(event) {
/*	var $cuentas_lista_plan = forms.cuentas_lista_plan
	Antes_imp($cuentas_lista_plan)
	globals.metodoPdf('cuentas_lista_plan','plan_de_cuentas - '+globals.glegajo,true)*/
	jasper('pdf')
}

/**
 * @properties={typeid:24,uuid:"468F21C6-2085-4C2E-8342-080B19288BC8"}
 */
function xls()
{
	jasper ('xls')
}

/**
 * @properties={typeid:24,uuid:"20A84157-849D-483A-A08E-F409C1F21ADB"}
 */
function csv()
{
	jasper('csv')
}

/**
 * @param {String} tipo
 *
 * @properties={typeid:24,uuid:"36774122-123B-46F0-BD2F-906FF8DDE193"}
 */
function jasper(tipo)
{
	var $antes = Antes_imp()
	if ($antes == -1)
	{
		globals.DIALOGS.showWarningDialog('Aviso','No hay cuentas para imprimir','OK')
		return
	}
	
	var $params = {
		userv: usuarios_to_uservs.razon_social
	}
	var $fs = databaseManager.getFoundSet('contable','cuentas')
	$fs.loadRecords($antes)
	$fs.sort('capitulo asc, subcapitulo asc, rubro asc, cuenta_formato asc')
	var $cols = ['capitulo','cuentas_to_capitulo.nombre','subcapitulo','cuentas_to_subcapitulo.descripcion','rubro','cuentas_to_rubro.descripcion','cuenta_impresion','marca_mon']
	var $ds = databaseManager.convertToDataSet($fs,$cols)
	$ds.setColumnName(2,'capitulo_desc')
	$ds.setColumnName(4,'subcapitulo_desc')
	$ds.setColumnName(6,'rubro_desc')
	globals.metodoJasper(tipo,'contable/plan_cuentas.jasper','Plan_Cuentas',$ds,$params)
}

/**
 * @properties={typeid:24,uuid:"6EC1F294-D9FF-4090-8A8D-E05B7FD7D2AE"}
 * @return {Object|JSDataSet}
 */
function Antes_imp() {
	var $sql = 'SELECT cuenta_id FROM cuentas WHERE empresa = ? '
	if (fListaSubCuentas == 0)
	{
		$sql += 'AND subcuenta = 0 '
	}
	if (fListaNoImprimir == 0)
	{
		$sql += 'AND marca_imprimir = 1 '
	}
	$sql += 'ORDER BY capitulo asc, subcapitulo asc, rubro asc, cuenta_formato asc'
	var $args 	= [globals.gUservID]
	var $ds 	= databaseManager.getDataSetByQuery('contable',$sql,$args,-1)
	
	if ($ds.getMaxRowIndex() == 0)
	{
		return -1
	}
	return $ds
}
